$nStart=0;
$nSkip=1;

printf "n	x0	dx0	lineArea	dlineArea	aFit	daFit	w	dw\n";
$order=$nStart;
while(1) {
# get mouse click
do {                 
print TO_ABS "-x mouseClick\n";$f=<FROM_ABS>;chomp $f;
#sleep 0.25 seconds  
select(undef, undef, undef, 0.25);
} while ($f==none);

($x1, $y1, $x2, $y2, $mode) = split(' ', $f, 5);

print TO_ABS "-x setFitRange -s$x1 -e$x2\n";
print TO_ABS "-x fitCurveIndex\n";
$fitCurve = <FROM_ABS>;
print TO_ABS "-x selectCurveByIndex -i $fitCurve \n";
print TO_ABS "-x statistic\n";
$text=<FROM_ABS>;
chomp $text;
($n, $min, $max, $sum, $std, $area, $name) = split(' ', $text, 7);

$baseLine = "$x1 $y1\r $x2 $y2";
print TO_ABS "-x replaceData -sBaseLine -p '$baseLine'\n";
$baseArea = ($x2-$x1)*($y1+$y2)/2;
$err = sqrt($sum)/$sum*$area;
$xmean = ($x2+$x1)/2;
$lineArea = $area - $baseArea;

$fitPara = "x1=$x1 fixed y1=$y1 fixed  x2=$x2 fixed y2=$y2 fixed  x0=$xmean";
print TO_ABS "-x setFormulaParameters -p '$fitPara'\n";
print TO_ABS "-x fit\n";
$text = <FROM_ABS>;
print TO_ABS "-x show\n";
# get parameter            
print TO_ABS "-x getFormulaParameters\n";
$p=<FROM_ABS>;
($x0, $dx0) = ($p =~ /x0= (\S+) \+- (\S+)/);
($a0, $da0) = ($p =~ /a0= (\S+) \+- (\S+)/);
($w, $dw) = ($p =~ /w= (\S+) \+- (\S+)/);
printf "%d  %g   %g  %g  %g  %g   %g  %g  %g\n", $order, $x0, $dx0, $lineArea, $err, $a0, $da0, $w, $dw;
$order++;
if ($nSkip!=1 && $order % $nSkip == 0) { $order++; }
}